package com.kingwood.algorithm.test;

import java.util.Arrays;
import java.util.Scanner;
import java.util.StringJoiner;

import static jdk.nashorn.internal.objects.NativeRegExp.source;

/**
 * @Author 22025812
 * @Description: 多段线数据压缩
 * https://blog.csdn.net/qfc_128220/article/details/134900877
 * @Date: 2024/3/1 13:44
 * @Modified By：
 * @Version 1.0
 */
public class CompressLine {
    public static String compressLine(int[] arr) {
        StringJoiner stringJoiner = new StringJoiner(" ");

        int preX = arr[0];
        int preY = arr[1];

        int preDirectX = 0;
        int preDirectY = 0;

        for (int i = 2; i < arr.length; i += 2) {
            int curX = arr[i];
            int curY = arr[i + 1];

            int offSetX = curX - preX;
            int offSetY = curY - preY;

            int base = Math.max(Math.abs(offSetX), Math.abs(offSetY));
            int directX = offSetX / base;
            int directY = offSetY / base;

            if (preDirectX != directX || preDirectY != directY) {
                stringJoiner.add(preX + " " + preY);
            }

            preX = curX;
            preY = curY;

            preDirectX = directX;
            preDirectY = directY;
        }

        stringJoiner.add(preX + " " + preY);

        return stringJoiner.toString();
    }

    public static String compressLine2(int[] arr) {
        StringJoiner stringJoiner = new StringJoiner(" ");

        int preX = arr[0];
        int preY = arr[1];
        int preDirectX = 0;
        int preDirectY = 0;

        for (int i = 2; i < arr.length; i += 2) {
            int curX = arr[i];
            int curY = arr[i + 1];

            int offSetX = curX - preX;
            int offSetY = curY - preY;

            int base = Math.max(Math.abs(offSetX), Math.abs(offSetY));
            int directX = offSetX / base;
            int directY = offSetY / base;

            if (preDirectX != directX || preDirectY != directY) {
                stringJoiner.add(preX + " " + preY);
            }

            preX = curX;
            preY = curY;

            preDirectX = directX;
            preDirectY = directY;
        }

        stringJoiner.add(preX + " " + preY);

        return stringJoiner.toString();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] arr = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        System.out.println(compressLine(arr));
        System.out.println(compressLine2(arr));
    }
}
